﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace G.Controls
{
    public class LogData
    {
        /// <summary>
        /// 产生错误的日期
        /// </summary>
        public DateTime Date { get; set; }
        /// <summary>
        /// 错误标题
        /// </summary>
        public string Title { get; set; }
        /// <summary>
        /// 错误消息
        /// </summary>
        public string Message { get; set; }
        /// <summary>
        /// 错误等级
        /// </summary>
        public int ErorrLevel { get; set; }

        public LogData()
        {
        }
        public LogData(DateTime date, string title, string message, int erorrLevel = 1)
        {
            this.Date = date;
            this.Title = title;
            this.Message = message;
            this.ErorrLevel = erorrLevel;
        }
        private const string IOKey = "Log_System_List";
        public static void Save(LogData logData)
        {
            List<LogData> logList;
            var settings = Windows.Storage.ApplicationData.Current.LocalSettings.Values;

            if (settings.ContainsKey(IOKey))
            {
                logList = settings[IOKey] as List<LogData>;
            }
            else
            {
                logList = new List<LogData>();
            }
            logList.Add(logData);
            settings[IOKey] = logList;
        }

        public static List<LogData> ReadLogData()
        {
            List<LogData> logList = null;
            var settings = Windows.Storage.ApplicationData.Current.LocalSettings.Values;
            if (settings.ContainsKey(IOKey))
            {
                logList = settings[IOKey] as List<LogData>;
            }
            return logList;
        }

        public static void Clear()
        {
            var settings = Windows.Storage.ApplicationData.Current.LocalSettings.Values;
            if (settings.ContainsKey(IOKey))
            {
                settings.Remove(IOKey);
            }
        }
    }
}
